<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="类型" prop="type">
        <el-input v-model="dataForm.type" placeholder="类型"></el-input>
      </el-form-item>
      <el-form-item label="管理费" prop="cost">
        <el-input v-model.number="dataForm.cost" placeholder="管理费"></el-input>
      </el-form-item>
      <el-form-item label="是否在用" prop="outorin">
        <el-switch
          v-model="dataForm.outorin"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="0"
          :inactive-value="1"
        ></el-switch>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        carId: 0,
        type: "",
        cost: "",
        outorin: "",
        state: "",
        createtime: "",
        updatetime: ""
      },
      dataRule: {
        type: [{ required: true, message: "类型不能为空", trigger: "blur" }],
        cost: [
          {
            validator: (rule, value, callback) => {
              if (!value) {
                return callback(new Error("管理费不能为空"));
              }
              setTimeout(() => {
                if (!Number.isInteger(value)) {
                  callback(new Error("请输入数字值"));
                } else {
                  if (value < 0) {
                    callback(new Error("管理费不能为负数"));
                  } else {
                    callback();
                  }
                }
              }, 1000);
            },
            trigger: "blur"
          }
        ],
        outorin: [
          { required: true, message: "进出状态不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm.carId = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.carId) {
          this.$http({
            url: this.$http.adornUrl(
              `/manager/car/info/${this.dataForm.carId}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.type = data.car.type;
              this.dataForm.cost = data.car.cost;
              this.dataForm.outorin = data.car.outorin;
              this.dataForm.state = data.car.state;
              this.dataForm.createtime = data.car.createtime;
              this.dataForm.updatetime = data.car.updatetime;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/manager/car/${!this.dataForm.carId ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              carId: this.dataForm.carId || undefined,
              type: this.dataForm.type,
              cost: this.dataForm.cost,
              outorin: this.dataForm.outorin,
              state: this.dataForm.state,
              createtime: this.dataForm.createtime,
              updatetime: this.dataForm.updatetime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
